﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using DVK.SP2007.Constants;

namespace DVK.SP2007
{
    /// <summary>
    /// This class is used by both ASP.NET and Timer Jobs.
    /// Currently caching is not used.
    /// 
    /// You can use GetSetting, which iterates all items.
    /// Or you can use QuerySetting, which queries exactly one setting. Depending on scenarion one or other is useful.
    /// 
    /// If you have the instance of SettingsManager and need several settings, then maybe quering all at 
    /// once and going them through several times to get several settings, makes sense (so use GetSetting).
    /// </summary>
    public class DvkSettingsRepository
    {
        private SPList _settingsList;
        private SPListItemCollection _settings;

        /// <summary>
        /// Initializes a new instance of the <see cref="DvkSettingsManager"/> class.
        /// </summary>
        /// <param name="web">The web.</param>
        public DvkSettingsRepository(SPWeb web)
        {
            _settingsList = web.GetList(Constants.Urls.ListSettings);
        }

        /// <summary>
        /// Loads all the settings (if they havent been loaded yet) and returns the one with the matching key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>The setting value.</returns>
        public string GetSetting(string key)
        {
            if (_settings == null)
                _settings = _settingsList.Items;

            foreach (SPListItem item in _settings)
            {
                var settingId = item[Fields.DvkSettingId.Name]as string;
                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(settingId) && settingId.Equals(key))
                    return item[Fields.DvkSettingValue.Name] as string;
            }
            return null;
        }

        /// <summary>
        /// Queries the setting item.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        private SPListItem QuerySettingItem(string key)
        {
            SPQuery query = new SPQuery();
            query.RowLimit = 1;
            query.Query =
                        "<Where>" +
                        "<Eq><FieldRef Name='" + Fields.DvkSettingId.Name + "' /><Value Type='Text'>" + key + "</Value></Eq>" +
                        "</Where>";
            SPListItemCollection items = _settingsList.GetItems(query);
            if (items.Count > 0)
                return items[0];
            return null;
        }

        /// <summary>
        /// Queries the setting by key from settings list.
        /// Does not load all items.
        /// </summary>
        /// <param name="key">The setting key.</param>
        /// <returns>The setting value.</returns>
        public string QuerySetting(string key)
        {
            var setting = QuerySettingItem(key);
            return (setting == null ? null : setting[Fields.DvkSettingValue.Name] as string);
        }

        /// <summary>
        /// Adds or updates new setting.
        /// </summary>
        public void AddOrUpdateSetting(string key, string value, string description)
        {
            var setting = QuerySettingItem(key);
            if (setting == null)
                setting = _settingsList.Items.Add();
            setting[Fields.DvkSettingId.Name] = key;
            setting[Fields.DvkSettingValue.Name] = value;
            setting[Fields.DvkSettingDescription.Name] = description;
            setting.Update();
        }
    }
}
